package com.qf.events.listeners;

import com.qf.events.AddUserEvent;
import com.qf.mappers.UserRoleMapper;
import com.qf.pojo.Role;
import com.qf.pojo.User;
import com.qf.pojo.UserRole;
import com.qf.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/*
杨威
2022/1/2
8:08
*/
@Component
public class AddUserListener {
    @Autowired
    UserRoleMapper userRoleMapper;
    @EventListener
    public void updateUserRoleTable(AddUserEvent addUserEvent){
        Role role = (Role) SecurityUtils.getRequest().getSession().getAttribute("role");
        User user = SecurityUtils.getUser();
        CompletableFuture.runAsync(()->{
            UserRole userRole=new UserRole();
            userRole.setUserId(user.getUId());
            userRole.setRoleId(role.getRoleId());
            System.out.println("userRole = " + userRole);
            userRoleMapper.insertUserRole(userRole);
        });

    }
}
